package interfaz;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import sistemaAlmacen.Articulo;


public class InterfazInventario extends JPanel implements IVista {
	private JTextField txtNombre;
	private JTextField txtCantidad;
	private JTable tblArticulos;
	private JButton btnBuscar;
	private JComboBox<String> cmbTipo;

	/**
	 * Create the panel.
	 */
	public InterfazInventario() {
		setBackground(Color.WHITE);
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0, 135, 35, 222, 0, 0, 0};
		gridBagLayout.rowHeights = new int[]{14, 15, 20, 10, 20, 10, 20, 30, 0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
		JLabel lblCriteriosDeBsqueda = new JLabel("Criterios de B\u00FAsqueda");
		lblCriteriosDeBsqueda.setFont(new Font("Tahoma", Font.PLAIN, 14));
		GridBagConstraints gbc_lblCriteriosDeBsqueda = new GridBagConstraints();
		gbc_lblCriteriosDeBsqueda.insets = new Insets(0, 0, 5, 5);
		gbc_lblCriteriosDeBsqueda.gridx = 2;
		gbc_lblCriteriosDeBsqueda.gridy = 0;
		add(lblCriteriosDeBsqueda, gbc_lblCriteriosDeBsqueda);
		
		JLabel lblNewLabel = new JLabel("Nombre");
		GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
		gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel.gridx = 2;
		gbc_lblNewLabel.gridy = 2;
		add(lblNewLabel, gbc_lblNewLabel);
		
		txtNombre = new JTextField();
		GridBagConstraints gbc_txtNombre = new GridBagConstraints();
		gbc_txtNombre.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtNombre.insets = new Insets(0, 0, 5, 5);
		gbc_txtNombre.gridx = 4;
		gbc_txtNombre.gridy = 2;
		add(txtNombre, gbc_txtNombre);
		txtNombre.setColumns(10);
		
		JLabel lblNewLabel_1 = new JLabel("Tipo");
		GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
		gbc_lblNewLabel_1.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_1.gridx = 2;
		gbc_lblNewLabel_1.gridy = 4;
		add(lblNewLabel_1, gbc_lblNewLabel_1);
		
		cmbTipo = new JComboBox<String>();
		cmbTipo.addItem(" ");
		GridBagConstraints gbc_cmbTipo = new GridBagConstraints();
		gbc_cmbTipo.fill = GridBagConstraints.HORIZONTAL;
		gbc_cmbTipo.insets = new Insets(0, 0, 5, 5);
		gbc_cmbTipo.gridx = 4;
		gbc_cmbTipo.gridy = 4;
		add(cmbTipo, gbc_cmbTipo);
		
		JLabel lblCantidad = new JLabel("Cantidad (\u2265)");
		GridBagConstraints gbc_lblCantidad = new GridBagConstraints();
		gbc_lblCantidad.insets = new Insets(0, 0, 5, 5);
		gbc_lblCantidad.gridx = 2;
		gbc_lblCantidad.gridy = 6;
		add(lblCantidad, gbc_lblCantidad);
		
		txtCantidad = new JTextField();
		GridBagConstraints gbc_txtCantidad = new GridBagConstraints();
		gbc_txtCantidad.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtCantidad.insets = new Insets(0, 0, 5, 5);
		gbc_txtCantidad.gridx = 4;
		gbc_txtCantidad.gridy = 6;
		add(txtCantidad, gbc_txtCantidad);
		txtCantidad.setColumns(8);
		
		btnBuscar = new JButton("Buscar");
		GridBagConstraints gbc_btnBuscar = new GridBagConstraints();
		gbc_btnBuscar.anchor = GridBagConstraints.EAST;
		gbc_btnBuscar.insets = new Insets(0, 0, 5, 5);
		gbc_btnBuscar.gridx = 4;
		gbc_btnBuscar.gridy = 8;
		add(btnBuscar, gbc_btnBuscar);
		
		tblArticulos = new JTable();
		

	}

	public String getTxtNombre() {
		return txtNombre.getText();
	}

	public String getTxtCantidad() {
		return txtCantidad.getText();
	}

	public String getCmbTipo() {
		return (String)cmbTipo.getSelectedItem();
	}

	@Override
	public void update() {
		this.invalidate();
		this.repaint();
		
	}

	@Override
	public void SetVisible(boolean visible) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void registerListener(ActionListener listener) {
		this.btnBuscar.addActionListener(listener);
	
	}
	
	public void populateTable(ArrayList<Articulo> articulos){
		
		Object contenido[][] = new Object[articulos.size()+1][3];
		
		int contadorFilas = 1;
		Iterator<Articulo> it = articulos.iterator();
		
		contenido[0][0] = "NOMBRE";
		contenido[0][1] = "TIPO";
		contenido[0][2] = "CANTIDAD";
		while(it.hasNext()){
			Articulo art = it.next();
			contenido[contadorFilas][0] = art.getNombre();
			contenido[contadorFilas][1] = art.getTipoArticulo();
			contenido[contadorFilas][2] = String.valueOf(art.getCantidad());
			
			contadorFilas++;
		}
		
		tblArticulos.setModel(new DefaultTableModel(
				contenido,
				new String[] {
					"Nombre", "Tipo", "Cantidad"
				}
			));
		
		tblArticulos.setBackground(Color.WHITE);
		tblArticulos.setFont(new Font("Arial", Font.PLAIN, 14));
		GridBagConstraints gbc_table = new GridBagConstraints();
		gbc_table.gridwidth = 5;
		gbc_table.insets = new Insets(0, 0, 0, 5);
		gbc_table.fill = GridBagConstraints.BOTH;
		gbc_table.gridx = 1;
		gbc_table.gridy = 10;
		add(tblArticulos, gbc_table);
	}
	
	public void PopulateComboTipoArticulo(ArrayList<Articulo> articulos){
		
		Iterator<Articulo> it = articulos.iterator();
		
		while(it.hasNext()){
			Articulo art = it.next();
			
			this.cmbTipo.setSelectedIndex(-1);
			this.cmbTipo.setSelectedItem(art.getTipoArticulo().toLowerCase());
			if(!(this.cmbTipo.getSelectedIndex() > -1))
				this.cmbTipo.addItem(art.getTipoArticulo().toLowerCase());
			
		}
		
	}
	
	

}
